#include "Comm.hpp"
#include "Fifo.hpp"

int main()
{
    // 1.获取key值
    key_t key = GetShmKeyOrDie();
    cout << "key:0x" << ToHex(key) << endl;

    // 2.创建共享内存
    int shmid = CreateShm(key, defaultsize);
    cout << "shmid:" << shmid << endl;

    // 3.挂接共享内存
    char *addr = (char *)AttachShm(shmid);
    cout << "addr:" << ToHex((uint64_t)addr) << endl;
    // DebugShm(shmid);

    // 5.通信
    Fifo fifo;
    Syns syn;
    syn.OpenReadOrDie();

    for (;;)
    {   
        if(!syn.Wait()) break; 
        cout << "Shm content " << addr << endl;
    }

    // sleep(10);
    //  4.去链接
    DetachShm(addr);
    std::cout << "Detach shm success, addr: " << ToHex((uint64_t)addr) << std::endl;
    // sleep(100);
    //  3.删除共享内存
    DeleteShm(shmid);

    return 0;
}